<?php
class Page
{
    function add()
    {
        global $template, $lang, $db;
        $error = 0;
        if(!$name = retrieve('POST','name',true)) $error++;
        if(!$contenue = retrieve('POST','contenue', true)) $error++;
        if($name == '') $error++;
        if($contenue == '')$error++;
        
        if($error == 0)
        {
            $QueryArray = array(
            'name'  =>  $name,
            'name_html' =>  htmlentities($name),
            'contenue'  =>  addslashes($contenue),
            'date_ajout'    =>  time(),
            'date_modif'    =>  time(),
            'auteur'    =>  $_SESSION['iduser'],
            );
            $db->sql_query(CreateQueryInsert($QueryArray, 'immo_page'));
            $template->assign_vars(array(
            'ERROR_MSG'	=> true,
            'ERRORH_CLASS'	=> 'error_success',
            'ERRORH_IMG'	=>	'comment_ok',
            'V_ERROR_TXT'	=>	$lang['Page_ajoutee']));
        }
        else
        {
            $template->assign_vars(array(
            'ERROR_MSG'	=> true,
            'ERRORH_CLASS'	=> 'error_warning',
            'ERRORH_IMG'	=>	'comment_warning',
            'V_ERROR_TXT'	=>	$lang['Page_non_ajoutee']));
        }
    }
    
    function edit()
    {
        global $template, $lang, $db;
        $error = 0;
        if(!$name = retrieve('POST','name',true)) $error++;
        if(!$contenue = retrieve('POST','contenue', true)) $error++;
        if(!$id = retrieve('POST','id', true)) $error++;
        if($name == '') $error++;
        if($contenue == '')$error++;
        if($id == '') $error++;
        
        if($error == 0)
        {
            $QueryArray = array(
            'name'  =>  $name,
            'name_html' =>  htmlentities($name),
            'contenue'  =>  addslashes($contenue),
            'date_modif'    =>  time(),
            );
            $db->sql_query(CreateQueryUpdate($QueryArray, 'immo_page'," id = '".$id."'"));
            $template->assign_vars(array(
            'ERROR_MSG'	=> true,
            'ERRORH_CLASS'	=> 'error_success',
            'ERRORH_IMG'	=>	'comment_ok',
            'V_ERROR_TXT'	=>	$lang['Page_modifiee']));
        }
        else
        {
            $template->assign_vars(array(
            'ERROR_MSG'	=> true,
            'ERRORH_CLASS'	=> 'error_warning',
            'ERRORH_IMG'	=>	'comment_warning',
            'V_ERROR_TXT'	=>	$lang['Page_non_modifiee']));
        } 
    }
    
    function delete($id)
    {
        global $template, $lang, $db;
        if($id == '') redirect('index.php');
        # recuperation du lien
        $Page = $db->sql_query("SELECT * FROM immo_page WHERE id = '". $id ."'");
        if($db->nb_resultat == 0) redirect('index.php');
        # Suppression lien si present
        $db->sql_query("DELETE FROM immo_menu_navigation WHERE lien = '". $Page->lien ."'");
        # Suppression de la base
        $db->sql_query("DELETE FROM immo_page WHERE id = '". $id ."'");
        $template->assign_vars(array(
        'ERROR_MSG'	=> true,
        'ERRORH_CLASS'	=> 'error_success',
        'ERRORH_IMG'	=>	'comment_ok',
        'V_ERROR_TXT'	=>	$lang['Page_supprimee']));
        
    }
    
    function menu_add($id)
    {
        global $template, $lang, $db;
        if($id == '') redirect('index.php');
        # recuperation du lien
        $Page = $db->sql_query("SELECT * FROM immo_page WHERE id = '". $id ."'",'OBJECT');
        if($db->nb_resultat == 0) redirect('index.php');
        # recuperation max ordre menu
        $Menu = $db->sql_query("SELECT MAX(ordre) AS ordremax FROM immo_menu_navigation",'OBJECT');
        $ordre_page = $Menu->ordremax + 1;
        # Insertion dans le menu
        $QueryArray = array(
        'titre' =>  $Page->name,
        'lien'  =>  'page.php?page='.$Page->id,
        'ordre' =>  $ordre_page,
        'actif' =>  '1',
        'description'   =>  $Page->name,
        );
        $db->sql_query(CreateQueryInsert($QueryArray,'immo_menu_navigation'));
        $template->assign_vars(array(
        'ERROR_MSG'	=> true,
        'ERRORH_CLASS'	=> 'error_success',
        'ERRORH_IMG'	=>	'comment_ok',
        'V_ERROR_TXT'	=>	$lang['Page_ajoutee_au_menu']));
    }
}